
import asyncio
from playwright.async_api import async_playwright, expect
import re
from slider_captcha_solver import SliderCaptchaSolver

async def main():
    solver = SliderCaptchaSolver()
    
    async with async_playwright() as p:
        # 启动浏览器
        browser = await p.chromium.launch(headless=False)  # 可见模式便于调试
        page = await browser.new_page()
        
        try:
            # 导航到测试页面（这里替换为实际需要测试的网址）
            await page.goto("https://account.youzan.com/login")
            
            await page.locator("div").filter(has_text=re.compile(r"^扫码登录$")).get_by_role("img").click()
            await page.get_by_text("密码登录").click()
            await page.locator("div").filter(has_text=re.compile(r"^忘记密码$")).locator("span").click()
            await page.get_by_role("textbox", name="请输入手机号").click()
            await page.get_by_role("textbox", name="请输入手机号").fill("18616235868")
            await page.get_by_role("textbox", name="输入登录密码").click()    
            await page.get_by_role("textbox", name="输入登录密码").fill("snfrex1314-")
            await page.get_by_role("checkbox", name="阅读并同意用户协议、隐私声明").check()
            await page.get_by_role("button", name="登 录").click()
            await page.locator("#slideIconRef").nth(1).click()
            await expect(page.get_by_text("向右拖动滑块填充拼图")).to_be_visible()                    
            
            # 解决滑块验证码
            # 使用用户提供的复杂CSS选择器
            success = await solver.solve_slider_captcha(
                page=page,
                background_selector="div:nth-child(30) > .slide-captcha-container > .slide-captcha > .slide-box > div:nth-child(2) > .content > .display > #slidePicRef > .bg",  # 验证码背景图选择器
                slider_selector="#blockRef:nth(1)",  # 滑块图片选择器，选择第二个匹配元素
                slider_handle_selector="#slideIconRef:nth(1)"  # 滑块拖动句柄选择器，选择第二个匹配元素
            )
            
            if success:
                print("滑块验证码验证成功，可以继续后续操作")
                # 继续执行后续操作
            else:
                print("滑块验证码验证失败，请检查选择器或手动处理")
                
        finally:
            # 关闭浏览器
            await browser.close()

if __name__ == "__main__":
    asyncio.run(main())
